﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using AppCommon.Framework;
using SysCommon.Business.CVSS.WS.BaseData;
using SysCommon.Entity.Business.SMS;
using SysCommon.Entity.CVSS.WS.BaseData;
using SysCommon.Interface.ManageUserSession;
using SysCommon.Tools;

namespace CVSS.Client.SMS.UI
{
    public class QueryCarPlateFormMDT
    {
        #region 变量
        QueryCarPlateForm frm=new QueryCarPlateForm();
        private Car_Ety[] m_cars_etys = null;
        private AreaCode_Ety[] area_etys;
        private CarMember_Ety[] carmember_etys;
        #endregion

        #region 委托与事件声明
        public delegate void AfterSelected(SendMessageRecceiver_Ety[] smr_etys);

        public AfterSelected OnAfterSeleceted;
        #endregion

        #region 初始化
        public void Initial()
        {
            try
            {
                frm.btn_Select.Click += new EventHandler(btn_Select_Click);
                frm.btn_Close.Click += new EventHandler(btn_Close_Click);
                frm.cmb_Area.TextChanged += new EventHandler(cmb_Area_TextChanged);
                frm.cmb_CarMember.TextChanged += new EventHandler(cmb_CarMember_TextChanged);
                frm.rdb_Area.CheckedChanged += new EventHandler(rdb_Area_CheckedChanged);
                frm.rdb_Carmember.CheckedChanged += new EventHandler(rdb_Carmember_CheckedChanged);
                frm.cmb_CarMember.EnabledChanged += new EventHandler(cmb_CarMember_EnabledChanged);
                frm.cmb_Area.EnabledChanged += new EventHandler(cmb_Area_EnabledChanged);
                frm.FormClosing += new FormClosingEventHandler(frm_FormClosing);

                SendMessageRecceiver_Ety[] etys = new SendMessageRecceiver_Ety[0];
                ShowDataView(etys);

                frm.rdb_Area.Checked = true;
                frm.cmb_CarMember.Enabled = false;
                frm.rdb_Carmember.Checked = false;

                InitialFilter();

                frm.tvw_Results.AfterCheck += new TreeViewEventHandler(tvw_Results_AfterCheck);
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用QueryCarPlateFormMDT.Initial出错！", ex);
            }
        }

        void frm_FormClosing(object sender, FormClosingEventArgs e)
        {
            frm.Hide();
            e.Cancel = true;
        }
        #endregion

        #region 显示窗体
        public void Show(IWin32Window owner)
        {
            try
            {
                //frm.btn_Select.Click += new EventHandler(btn_Select_Click);
                //frm.btn_Close.Click += new EventHandler(btn_Close_Click);
                //frm.cmb_Area.TextChanged += new EventHandler(cmb_Area_TextChanged);
                //frm.cmb_CarMember.TextChanged += new EventHandler(cmb_CarMember_TextChanged);
                //frm.rdb_Area.CheckedChanged += new EventHandler(rdb_Area_CheckedChanged);
                //frm.rdb_Carmember.CheckedChanged += new EventHandler(rdb_Carmember_CheckedChanged);
                //frm.cmb_CarMember.EnabledChanged += new EventHandler(cmb_CarMember_EnabledChanged);
                //frm.cmb_Area.EnabledChanged += new EventHandler(cmb_Area_EnabledChanged);

                //SendMessageRecceiver_Ety[] etys=new SendMessageRecceiver_Ety[0];
                //ShowDataView(etys);

                //frm.rdb_Area.Checked = true;
                //frm.cmb_CarMember.Enabled = false;
                //frm.rdb_Carmember.Checked = false;

                //InitialFilter();

                //frm.tvw_Results.AfterCheck += new TreeViewEventHandler(tvw_Results_AfterCheck);

                frm.ShowDialog(owner);
            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用QueryCarPlateFormMDT.Show出错！", ex);
            }
        }
        #endregion

        #region 选择节点事件处理
        private bool m_bNodeCheck = true;
        void tvw_Results_AfterCheck(object sender, TreeViewEventArgs e)
        {
            try
            {
                if (!m_bNodeCheck) return;
                m_bNodeCheck = false;
                TreeNode currentNode = e.Node;
                if(currentNode.Nodes.Count>0)
                {
                    bool re = CheckNodes(ref currentNode);
                }

                else
                {
                    currentNode = currentNode.Parent;
                    int count = currentNode.Nodes.Count;
                    int true_count = 0;
                    int false_count = 0;
                    for(int i=0;i<count;i++)
                    {
                        if (currentNode.Nodes[i].Checked)
                            true_count++;
                        else
                            false_count++;
                    }
                    if (true_count == count) currentNode.Checked = true;
                    if (false_count == count) currentNode.Checked = false;

                }
                m_bNodeCheck = true;
            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用QueryCarPlateFormMDT.tvw_Result_AfterCheck出错！",ex);
            }
        }
        #endregion

        #region 窗体事件函数
        void cmb_CarMember_EnabledChanged(object sender, EventArgs e)
        {
            if(frm.cmb_CarMember.Enabled)
                cmb_CarMember_TextChanged(null,null);
        }

        void cmb_Area_EnabledChanged(object sender, EventArgs e)
        {
            if(frm.cmb_Area.Enabled)
                cmb_Area_TextChanged(null, null);
        }

        void rdb_Carmember_CheckedChanged(object sender, EventArgs e)
        {
            frm.cmb_CarMember.Enabled = frm.rdb_Carmember.Checked;
            frm.cmb_Area.Enabled = frm.rdb_Area.Checked;
        }

        void rdb_Area_CheckedChanged(object sender, EventArgs e)
        {
            frm.cmb_CarMember.Enabled = frm.rdb_Carmember.Checked;
            frm.cmb_Area.Enabled = frm.rdb_Area.Checked;
        }
        #endregion

        #region 节点检查/判断父节点是否应该全选或全不选
        private bool CheckNodes(ref TreeNode currentNode)
        {
            try
            {
                TreeNodeCollection treeNodeColl = currentNode.Nodes;
                if (treeNodeColl.Count == 0) return false;
                TreeNode tmpNode = currentNode;
                for (int i = 0; i < treeNodeColl.Count; i++)
                {
                    currentNode = tmpNode;
                    treeNodeColl[i].Checked = currentNode.Checked;
                    currentNode = treeNodeColl[i];
                    if (!CheckNodes(ref currentNode)) currentNode = tmpNode;
                }

                return true;
            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用QueryCarPlateFormMDT.CheckNodes出错！", ex);
                return false;
            }
        }
        #endregion

        #region 车队查询
        void cmb_CarMember_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (!frm.rdb_Carmember.Checked) return;

                string carmember_name = frm.cmb_CarMember.Text;
                if(carmember_name=="全部")
                {
                    //填充所有车队列表
                    frm.tvw_Results.Nodes.Clear();
                    foreach (CarMember_Ety carMember_Ety in carmember_etys)
                    {
                        frm.tvw_Results.Nodes.Add(carMember_Ety.ID.ToString(), carMember_Ety.NAME);
                        
                    }

                    //填充车辆
                    foreach (Car_Ety car_Ety in m_cars_etys)
                    {
                        try
                        {
                            frm.tvw_Results.Nodes[car_Ety.CARMEMBERID.ToString()].Nodes.Add(car_Ety.CARPLATE);
                            //node.Name = car_Ety.CARMEMBERID.ToString();
                        }
                        catch
                        {
                            string carmember_str = "";
                            foreach (CarMember_Ety ety in carmember_etys)
                            {
                                if(ety.ID.ToString()==car_Ety.CARMEMBERID.ToString())
                                {
                                    carmember_str = ety.NAME;
                                    break;
                                }
                            }
                            
                            frm.tvw_Results.Nodes.Add(car_Ety.CARMEMBERID.ToString(), carmember_str);
                            frm.tvw_Results.Nodes[car_Ety.CARMEMBERID.ToString()].Nodes.Add(car_Ety.CARPLATE);
                            //node.Name = car_Ety.CARMEMBERID.ToString();
                        }
                        //}
                    }
                }
                else
                {
                    frm.tvw_Results.Nodes.Clear();
                    frm.tvw_Results.Nodes.Add(carmember_name);
                    //获取车队ID
                    int carmemberid = -1;
                    foreach (CarMember_Ety carmember_ety in carmember_etys)
                    {
                        if(carmember_ety.NAME==carmember_name)
                        {
                            carmemberid = carmember_ety.ID;
                            break;
                        }
                    }
                    if(carmemberid==-1)return;
                    //填充车辆列表
                    foreach (Car_Ety ety in m_cars_etys)
                    {
                        if(ety.CARMEMBERID==carmemberid)
                        {
                            frm.tvw_Results.Nodes[0].Nodes.Add(ety.CARPLATE);
                            //node.Name = ety.CARMEMBERID.ToString();
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用QueryCarPlateFormMDT.cmb_CarMember_TextChanged出错！", ex);
            }
        }
        #endregion

        #region 区域查询
        void cmb_Area_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (!frm.rdb_Area.Checked) return;

                string area_name = frm.cmb_Area.Text;
                if (area_name == "全部")
                {
                    //GetCarsByArea();
                    area_name = "广东省";
                }
                //else
                //{
                    frm.tvw_Results.Nodes.Clear();
                    string p_areaCode = "440000";
                    foreach (AreaCode_Ety ety in area_etys)
                    {
                        if(ety.NAME==area_name)
                        {
                            p_areaCode = ety.CODE;
                            break;
                        }
                    }
                    GetCarsByArea(p_areaCode,area_name);
                //}
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用QueryCarPlateFormMDT.cmb_Area_TextChanged出错！", ex);
            }
        }
        #endregion

        void btn_Close_Click(object sender, EventArgs e)
        {
            try
            {
                frm.Hide();
            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用QueryCarPlateFormMDT.btn_Close_Click出错！", ex);
            }
        }

        void btn_Select_Click(object sender, EventArgs e)
        {
            try
            {
                TreeNodeCollection treeNodeColl = frm.tvw_Results.Nodes;
                ArrayList nodeList = new ArrayList();
                int node_count = treeNodeColl.Count;
                for(int i=0;i<node_count;i++)
                {
                    if(IsChildNode(treeNodeColl[i]))
                    {
                        //nodeList.Add(treeNodeColl[i]);
                        continue;
                    }
                    else
                    {
                        nodeList = AddNodeToList(treeNodeColl[i], nodeList);
                    }
                }
                SendMessageRecceiver_Ety[] etys=new SendMessageRecceiver_Ety[nodeList.Count];
                TreeNode tmpNode;
                for (int i = 0; i < nodeList.Count;i++ )
                {
                    etys[i]=new SendMessageRecceiver_Ety();
                    tmpNode = (TreeNode) nodeList[i];
                    etys[i].ID = i + 1;
                    etys[i].CARPLATE = tmpNode.Text;
                    etys[i].CARMEMBER = tmpNode.Parent.Text;
                }
                OnAfterSeleceted(etys);
                frm.Hide();
            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用QueryCarPlateFormMDT.btn_Select_Click出错！", ex);
            }
        }
        ArrayList AddNodeToList(TreeNode inNode,ArrayList inList)
        {
            TreeNode node = inNode;
            ArrayList list = inList;

            int count = inNode.Nodes.Count;
            if (count == 0)
            {
                if(node.Checked)
                {
                    if(frm.rdb_Area.Checked)
                    {
                        string str = node.Text;
                        if (str.Substring(str.Length - 1, 1) == "市" | str.Substring(str.Length - 1, 1) == "省")
                            return list;
                    }
                    list.Add(node);
                }
                return list;
            }
            else
            {
                for(int i=0;i<count;i++)
                {
                    list = AddNodeToList(node.Nodes[i], list);
                }
                return list;
            }
        }
        bool IsChildNode(TreeNode inNode)
        {
            try
            {
                if (inNode.Nodes.Count == 0) return true;
                else return false;
            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用QueryCarPlateFormMDT.GetSelectedNode出错！", ex);
            }
            return false;
        }
        string GetCarMember(int memberid)
        {
            foreach (CarMember_Ety ety in carmember_etys)
            {
                if(memberid==ety.ID)
                {
                    return ety.NAME;
                }
            }
            return "";
        }
        #region 显示数据
        private void ShowDataView(object[] etys)
        {
            try
            {
                string[] cnColumns ={ "序号", "车牌", "车队" };
                string[] enColumns ={ "ID", "CARPLATE", "CARMEMBER" };

                ShowEntityForDataGridView.SetDefaultDataGrigStyle(frm.dg_DataView);
                
                ShowEntityForDataGridView.EntityToView(cnColumns, enColumns, etys, frm.dg_DataView);

                frm.dg_DataView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用ShowDataView出错", ex);
            }
        }
        #endregion

        #region 填充全部辖区车辆信息
        private void GetCarsByAllArea()
        {
            frm.tvw_Results.Nodes.Clear();
            frm.cmb_Area.Items.Clear();
            frm.cmb_Area.Items.Add("全部");
            foreach (AreaCode_Ety area_ety in area_etys)
            {
                frm.cmb_Area.Items.Add(area_ety.NAME);

                //填充查询结果
                string temp_code = area_ety.CODE;
                int code_length = temp_code.Length;
                string sub_code = temp_code.Substring(code_length - 4, 4);
                if (sub_code == "0000")
                {
                    if (frm.tvw_Results.Nodes.Count == 0)
                    {
                        frm.tvw_Results.Nodes.Add(area_ety.CODE, area_ety.NAME);
                    }

                    continue;
                }
                if (temp_code.Substring(code_length - 2, 2) == "00")
                {
                    if (frm.tvw_Results.Nodes.Count == 0)
                    {
                        frm.tvw_Results.Nodes.Add("440000", "广东省");
                    }
                    string current_code = temp_code.Substring(0, 4) + "00";
                    TreeNode treeNode = new TreeNode(current_code);
                    if (frm.tvw_Results.Nodes[0].Nodes.Contains(treeNode)) continue;
                    frm.tvw_Results.Nodes[0].Nodes.Add(current_code, area_ety.NAME);
                }
                //else
                //{
                //    if (frm.tvw_Results.Nodes.Count == 0)
                //    {
                //        frm.tvw_Results.Nodes.Add("440000", "广东省");
                //    }

                //    string current_code = temp_code.Substring(0, 4) + "00";
                //    try
                //    {
                //        frm.tvw_Results.Nodes[0].Nodes[current_code].Nodes.Add(area_ety.CODE, area_ety.NAME);
                //    }
                //    catch
                //    {
                //        foreach (AreaCode_Ety areaCode_Ety in area_etys)
                //        {
                //            if (areaCode_Ety.CODE == current_code)
                //            {
                //                frm.tvw_Results.Nodes[0].Nodes[current_code].Nodes.Add(areaCode_Ety.CODE, areaCode_Ety.NAME);
                //                frm.tvw_Results.Nodes[0].Nodes[current_code].Nodes.Add(area_ety.CODE, area_ety.NAME);
                //                //break;
                //            }
                //        }
                //    }
                //}
            }
        }

        private void GetCarsByArea()
        {
            frm.tvw_Results.Nodes.Clear();
            foreach (AreaCode_Ety area_ety in area_etys)
            {
                frm.cmb_Area.Items.Add(area_ety.NAME);

                //填充查询结果
                string temp_code = area_ety.CODE;
                int code_length = temp_code.Length;
                string sub_code = temp_code.Substring(code_length - 4, 4);
                if (sub_code == "0000")
                {
                    if (frm.tvw_Results.Nodes.Count == 0)
                    {
                        frm.tvw_Results.Nodes.Add(area_ety.CODE, area_ety.NAME);
                    }

                    continue;
                }
                if (temp_code.Substring(code_length - 2, 2) == "00")
                {
                    if (frm.tvw_Results.Nodes.Count == 0)
                    {
                        frm.tvw_Results.Nodes.Add("440000", "广东省");
                    }
                    string current_code = temp_code.Substring(0, 4) + "00";
                    TreeNode treeNode = new TreeNode(current_code);
                    if (frm.tvw_Results.Nodes[0].Nodes.Contains(treeNode)) continue;
                    frm.tvw_Results.Nodes[0].Nodes.Add(current_code, area_ety.NAME);
                }
            }
        }

        private void GetCarsByArea(string str_areaCode,string str_areaName)
        {
            frm.tvw_Results.Nodes.Clear();

                if (str_areaCode == "440000")
                {
                    GetCarsByArea();
                    try
                    {
                        frm.tvw_Results.Nodes[0].Expand();
                    }
                    catch
                    {
                    }
                    //填充车辆列表
                    foreach (Car_Ety car_Ety in m_cars_etys)
                    {
                        if (car_Ety.AREACODE == null) continue;
                        string car_temp_areacode = car_Ety.AREACODE;
                        string city_temp_areacode = car_temp_areacode.Substring(0, 4) + "00";
                        try
                        {
                            frm.tvw_Results.Nodes[0].Nodes[city_temp_areacode].Nodes.Add(car_Ety.CARPLATE);
                        }
                        catch (Exception ex)
                        {
                            SysCommon.Global.mainLog.Error("调用QueryCarPlateFormMDT.InitialFilter填充车辆列表时出错！", ex);
                        }
                    }
                }
                else
                {
                    frm.tvw_Results.Nodes.Clear();
                    frm.tvw_Results.Nodes.Add(str_areaCode, str_areaName);
                    frm.tvw_Results.Nodes[0].Expand();
                    //填充车辆列表
                    foreach (Car_Ety car_Ety in m_cars_etys)
                    {
                        if (car_Ety.AREACODE == null) continue;
                        string car_temp_areacode = car_Ety.AREACODE;
                        string city_temp_areacode = car_temp_areacode.Substring(0, 4) + "00";
                        
                        try
                        {
                            if (car_temp_areacode==str_areaCode|city_temp_areacode == str_areaCode)
                            {
                                frm.tvw_Results.Nodes[0].Nodes.Add(car_Ety.CARPLATE);
                            }
                        }
                        catch (Exception ex)
                        {
                            SysCommon.Global.mainLog.Error("调用QueryCarPlateFormMDT.InitialFilter填充车辆列表时出错！", ex);
                        }
                    }

                }
        }
        #endregion

        #region 根据车队获取车辆信息
        private void GetCarsByCarMember(string carmember_name)
        {
            
        }
        #endregion

        #region 初始化选择项
        private void InitialFilter()
        {
            try
            {
                frm.rdb_Area.Checked = true;
                frm.rdb_Carmember.Checked = false;

                WS_BaseData_BizCtrlImpl ws_base = new WS_BaseData_BizCtrlImpl();

                frm.tvw_Results.CheckBoxes = true;

                //获取所有辖区列表
                area_etys = ws_base.GetAllAreaInfo();
                //获取所有车队列表
                carmember_etys = ws_base.GetAllCarMemberInfo();
                //获取所有车辆列表
                m_cars_etys = ws_base.GetAllCarInfo();

                //填充辖区列表
                GetCarsByAllArea();

                frm.cmb_Area.Text = "全部";
                //填充车队列表
                frm.cmb_CarMember.Items.Clear();
                frm.cmb_CarMember.Items.Add("全部");
                foreach (CarMember_Ety carMember_Ety in carmember_etys)
                {
                    frm.cmb_CarMember.Items.Add(carMember_Ety.NAME);
                    
                }
                frm.cmb_CarMember.Text = "全部";
                //填充车辆列表
                foreach (Car_Ety car_Ety in m_cars_etys)
                {
                    if (car_Ety.AREACODE == null) continue;
                    string car_temp_areacode = car_Ety.AREACODE;
                    string city_temp_areacode = car_temp_areacode.Substring(0, 4) + "00";
                    try
                    {
                        frm.tvw_Results.Nodes[0].Nodes[city_temp_areacode].Nodes.Add(car_Ety.CARPLATE);
                    }
                    catch(Exception ex)
                    {
                        SysCommon.Global.mainLog.Error("调用QueryCarPlateFormMDT.InitialFilter填充车辆列表时出错！", ex);
                    }
                }

                frm.tvw_Results.Nodes[0].Expand();
            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用QueryCarPlateFormMDT.InitialFilter出错！", ex);
            }
        }
        #endregion

        #region 初始化选择项(webservice压缩处理)
        /// <summary>
        /// 初始化选择项(webservice压缩处理)
        /// </summary>
        private void InitialFilter2()
        {     
            
            try
            {
                SysCommon.Entity.Vehicle.Car_Ety[] m_cars_etys = null;

                //SysCommon.Entity.Business.AreaCode_Ety[] area_etys = null;

                SysCommon.Entity.Human.CarMember_Ety[] carmember_etys = null;
                frm.rdb_Area.Checked = true;
                frm.rdb_Carmember.Checked = false;

                WS_BaseData_BizCtrlImpl ws_base = new WS_BaseData_BizCtrlImpl();

                frm.tvw_Results.CheckBoxes = true;

                //获取所有辖区列表
                area_etys = ws_base.GetAllAreaInfo();
                //获取所有车队列表
               byte[] zipBuffer2= ws_base.GetAllCarMemberInfo2();
               carmember_etys = (SysCommon.Entity.Human.CarMember_Ety[])CompressionHelper.DeCompressEntity(zipBuffer2); 
                //获取所有车辆列表
               byte[] zipBuffer3 = ws_base.GetAllCarInfo2(); ;
               m_cars_etys = (SysCommon.Entity.Vehicle.Car_Ety[])CompressionHelper.DeCompressEntity(zipBuffer3); 

                //填充辖区列表
                GetCarsByAllArea();

                frm.cmb_Area.Text = "全部";
                //填充车队列表
                frm.cmb_CarMember.Items.Clear();
                frm.cmb_CarMember.Items.Add("全部");
                foreach (SysCommon.Entity.Human.CarMember_Ety carMember_Ety in carmember_etys)
                {
                    frm.cmb_CarMember.Items.Add(carMember_Ety.NAME);
                    
                }
                frm.cmb_CarMember.Text = "全部";
                //填充车辆列表
                foreach (SysCommon.Entity.Vehicle.Car_Ety car_Ety in m_cars_etys)
                {
                    if (car_Ety.AREACODE == null) continue;
                    string car_temp_areacode = car_Ety.AREACODE;
                    string city_temp_areacode = car_temp_areacode.Substring(0, 4) + "00";
                    try
                    {
                        frm.tvw_Results.Nodes[0].Nodes[city_temp_areacode].Nodes.Add(car_Ety.CARPLATE);
                    }
                    catch(Exception ex)
                    {
                        SysCommon.Global.mainLog.Error("调用QueryCarPlateFormMDT.InitialFilter填充车辆列表时出错！", ex);
                    }
                }

                frm.tvw_Results.Nodes[0].Expand();
            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用QueryCarPlateFormMDT.InitialFilter出错！", ex);
            }
        }
        #endregion

        public bool IsSelectByArea
        {
            get { return frm.cmb_Area.Enabled; }
        }
    }
}
